import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.*;
import java.io.*;

public class EditorFrame extends JFrame {
    private static final int FRAME_WIDTH = 820;
    private static final int FRAME_HEIGHT = 400;
    private static final int FIELD_WIDTH = 5;

    private JFrame frame;
    private String path;

    private ActionListener listener;
    private JTextField PathField;
    private JComboBox<String> CharacterSelect;
    private JLabel NameLabel;
    private JTextField LevelField;
    private JTextField curHealthField;
    private JTextField maxHealthField;
    private JTextField curMagicField;
    private JTextField maxMagicField;
    private JTextField ExpField;
    private JTextField StrengthField;
    private JTextField SpeedField;
    private JTextField VitalityField;
    private JTextField MagicField;
    private JTextField GilField;
    
    private JLabel CommandLabel1;
    private JLabel CommandLabel2;
    private JLabel CommandLabel3;
    private JLabel CommandLabel4;

    private JComboBox<String> EsperCombo;
	private JComboBox<String> RightItemCombo;
	private JComboBox<String> LeftItemCombo;
	private JComboBox<String> HelmetCombo;
	private JComboBox<String> ArmorCombo;
	private JComboBox<String> Relic1Combo;
	private JComboBox<String> Relic2Combo;

	private byte StatusByte;
	private JCheckBox DarknessBox;
	private JCheckBox ZombieBox;
	private JCheckBox PoisonBox;
	private JCheckBox MagiTekBox;
	private JCheckBox InvisibleBox;
	private JCheckBox KappaBox;
	private JCheckBox StoneBox;
	private JCheckBox WoundedBox;
	private JCheckBox FloatBox;

    private JButton saveButton;
    private JButton cancelButton;

    private sseditor SaveState;

	public EditorFrame() 
		throws FileNotFoundException, IOException {
		SaveState = new sseditor();

		class ChoiceListener implements ActionListener {
			public void actionPerformed(ActionEvent event) {
				try{ 
					Update();
				}
				catch(IOException e) {
					System.exit(1);
				}
			}
		}

		listener = new ChoiceListener();

		createControlPanel();
		
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
	}		

	public void createControlPanel() throws FileNotFoundException, IOException {
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new BorderLayout());
		JPanel TopPanel = new JPanel();
		JPanel NWPanel = new JPanel();
		JLabel PathLabel = new JLabel("Full Path: ");
		PathField = new JTextField(40);
		PathField.setText("Insert full path to Save State here");
		JPanel PathPanel = new JPanel();
		PathPanel.setLayout(new BorderLayout());
		PathPanel.add(PathLabel, BorderLayout.WEST);
		JPanel PathFieldPanel = new JPanel();
		PathFieldPanel.add(PathField);
		PathPanel.add(PathFieldPanel, BorderLayout.CENTER);

		JButton loadButton = new JButton("Load");
		
		class LoadListener implements ActionListener {
			public void actionPerformed(ActionEvent event) {
				path = (String) PathField.getText();
				
				try{
					SaveState.fileRead(path);
					Update();
				}
				catch(IOException e) {
					System.exit(1);
				}

			}
		}
		
		ActionListener loadClick = new LoadListener();
		loadButton.addActionListener(loadClick);

		JPanel loadButtonPanel = new JPanel();
		loadButtonPanel.add(loadButton);
		PathPanel.add(loadButtonPanel, BorderLayout.EAST);

		NWPanel.setLayout(new GridLayout(2,1));
		NWPanel.add(PathPanel);
		
		JPanel SelectionPanel = createComboBox();
		NWPanel.add(SelectionPanel);
		TopPanel.setLayout(new BorderLayout());
		TopPanel.add(NWPanel, BorderLayout.WEST);
		controlPanel.add(TopPanel, BorderLayout.NORTH);
		setCharacter();

		JPanel TextPanel1 = createNameField();
		JPanel HealthPanel = createHealthPanel();
		JPanel LevelPanel = createLevelPanel();
		JPanel MPPanel = createMPPanel();
		JPanel ExpPanel = createExpPanel();
		JPanel StrengthPanel = createStrengthPanel();
		JPanel SpeedPanel = createSpeedPanel();
		JPanel VitalityPanel = createVitalityPanel();
		JPanel MagicPanel = createMagicPanel();
		JPanel GilPanel = createGilPanel();

		JPanel StatusPanel = createStatusPanel();
		JPanel StatusWrapper = new JPanel();

		StatusWrapper.setLayout(new BorderLayout());
		StatusWrapper.add(StatusPanel, BorderLayout.NORTH);
		

		JPanel NamePanel = new JPanel();
		NamePanel.setLayout(new GridLayout(5, 2));
		NamePanel.add(TextPanel1);
		NamePanel.add(StrengthPanel);
		NamePanel.add(LevelPanel);
		NamePanel.add(SpeedPanel);
		NamePanel.add(HealthPanel);
		NamePanel.add(VitalityPanel);
		NamePanel.add(MPPanel);
		NamePanel.add(MagicPanel);
		NamePanel.add(ExpPanel);
		NamePanel.add(GilPanel);
	
		JPanel ItemPanel = createItemPanel();

		StatusWrapper.add(ItemPanel, BorderLayout.SOUTH);
		JPanel NameWrapper = new JPanel();
		NameWrapper.setLayout(new BorderLayout());
		NameWrapper.add(NamePanel, BorderLayout.NORTH);
		controlPanel.add(NameWrapper, BorderLayout.WEST);
		controlPanel.add(StatusWrapper, BorderLayout.EAST);
		//controlPanel.add(ItemPanel, BorderLayout.EAST);
		JPanel Buttons = createButton();
		JPanel ButtonPanel = new JPanel();
		ButtonPanel.setLayout(new BorderLayout());
		ButtonPanel.add(Buttons, BorderLayout.EAST);
		controlPanel.add(ButtonPanel, BorderLayout.SOUTH);
		add(controlPanel);

	}

	private JPanel createComboBox() {
		JLabel SelectLabel = new JLabel("Choose character to edit: ");
		CharacterSelect = new JComboBox<String>();

		CharacterSelect.addItem("Tina");
		CharacterSelect.addItem("Lock");
		CharacterSelect.addItem("Cayene");
		CharacterSelect.addItem("Shadow");
		CharacterSelect.addItem("Edgar");
		CharacterSelect.addItem("Mash");
		CharacterSelect.addItem("Celes");
		CharacterSelect.addItem("Strago");
		CharacterSelect.addItem("Relm");
		CharacterSelect.addItem("Setzer");
		CharacterSelect.addItem("Mog");
		CharacterSelect.addItem("Gau");
		CharacterSelect.addItem("Gogo");
		CharacterSelect.addItem("Umaro");
		
		CharacterSelect.setEditable(false);
		CharacterSelect.addActionListener(listener);

		JPanel SelectPanel = new JPanel();
		SelectPanel.add(SelectLabel);
		SelectPanel.add(CharacterSelect);
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.add(SelectPanel, BorderLayout.WEST);
		return panel;
	} 

	private void setCharacter() {
		// throws FileNotFoundException, IOException {
		String Character = (String) CharacterSelect.getSelectedItem();
		SaveState.SetOffset(Character);
 	}

	private JPanel createNameField() {
		NameLabel = new JLabel("Name: EMPTY");

		JPanel panel = new JPanel();
		panel.add(NameLabel);
		return panel;
	}

	private void Update() throws IOException {
		String Character = (String) CharacterSelect.getSelectedItem();
		SaveState.SetOffset(Character);
		SaveState.fileRead(path);
		NameLabel.setText("Name: " + SaveState.getCharName());
		LevelField.setText("" + SaveState.getLevel());
		curHealthField.setText("" + SaveState.getHP());
		maxHealthField.setText("" + SaveState.getMaxHP());
		maxMagicField.setText("" + SaveState.getMaxMP());
		curMagicField.setText("" + SaveState.getMP());		
		ExpField.setText("" + SaveState.getExp());
		StrengthField.setText("" + SaveState.getStrength());
		SpeedField.setText("" + SaveState.getSpeed());
		VitalityField.setText("" + SaveState.getVitality());
		MagicField.setText("" + SaveState.getMagic());
		GilField.setText("" + SaveState.getGil());

		EsperCombo.setSelectedItem(SaveState.getEsper());
		RightItemCombo.setSelectedItem(SaveState.getRightItem());
		LeftItemCombo.setSelectedItem(SaveState.getLeftItem());
		HelmetCombo.setSelectedItem(SaveState.getHelmetItem());
		ArmorCombo.setSelectedItem(SaveState.getArmorItem());
		Relic1Combo.setSelectedItem(SaveState.getRelic1Item());
		Relic2Combo.setSelectedItem(SaveState.getRelic2Item());

		StatusByte = SaveState.printStatus();
		DarknessBox.setSelected((StatusByte & (byte)0x01) == (byte)0x01);
		ZombieBox.setSelected((StatusByte & (byte)0x02) == (byte)0x02);
		PoisonBox.setSelected((StatusByte & (byte)0x04) == (byte)0x04);
		MagiTekBox.setSelected((StatusByte & (byte)0x08) == (byte)0x08);
		InvisibleBox.setSelected((StatusByte & (byte)0x10) == (byte)0x10);
		KappaBox.setSelected((StatusByte & (byte)0x20) == (byte)0x20);
		StoneBox.setSelected((StatusByte & (byte)0x40) == (byte)0x40);
		WoundedBox.setSelected((StatusByte & (byte)0x80) == (byte)0x80);
		FloatBox.setSelected(SaveState.getFloat());

	}

	private JPanel createLevelPanel() {
		JLabel LevelLabel = new JLabel("Level: ");
		LevelField = new JTextField(2);

		JPanel panel = new JPanel();
		panel.add(LevelLabel);
		panel.add(LevelField);
		return panel;
	}

	private JPanel createHealthPanel() {
		JLabel HealthLabel = new JLabel("HP: ");
		curHealthField = new JTextField(3);
		maxHealthField = new JTextField(3);
		JLabel slash = new JLabel("/");

		JPanel panel = new JPanel();
		panel.add(HealthLabel);
		panel.add(curHealthField);
		panel.add(slash);
		panel.add(maxHealthField);
		return panel;
	}

	private JPanel createMPPanel() {
		JLabel MagicLabel = new JLabel("MP: ");
		curMagicField = new JTextField(2);
		maxMagicField = new JTextField(2);
		JLabel slash = new JLabel("/");

		JPanel panel = new JPanel();
		panel.add(MagicLabel);
		panel.add(curMagicField);
		panel.add(slash);
		panel.add(maxMagicField);
		return panel;
	}

	private JPanel createExpPanel() {
		JLabel ExpLabel = new JLabel("Exp: ");
		ExpField = new JTextField(FIELD_WIDTH);

		JPanel panel = new JPanel();
		panel.add(ExpLabel);
		panel.add(ExpField);
		return panel;
	}

	private JPanel createGilPanel() {
		JLabel GilLabel = new JLabel("Gil: ");
		GilField = new JTextField(FIELD_WIDTH);

		JPanel panel = new JPanel();
		panel.add(GilLabel);
		panel.add(GilField);
		return panel;
	}

	private JPanel createStrengthPanel() {
		JLabel StrengthLabel = new JLabel("Strength: ");
		StrengthField = new JTextField(3);

		JPanel panel = new JPanel();
		panel.add(StrengthLabel);
		panel.add(StrengthField);
		return panel;
	}

	private JPanel createSpeedPanel() {
		JLabel SpeedLabel = new JLabel("Speed: ");
		SpeedField = new JTextField(3);

		JPanel panel = new JPanel();
		panel.add(SpeedLabel);
		panel.add(SpeedField);
		return panel;
	}

	private JPanel createVitalityPanel() {
		JLabel VitalityLabel = new JLabel("Vitality: ");
		VitalityField = new JTextField(3);

		JPanel panel = new JPanel();
		panel.add(VitalityLabel);
		panel.add(VitalityField);
		return panel;
	}

	private JPanel createMagicPanel() {
		JLabel MagicLabel = new JLabel("Magic: ");
		MagicField = new JTextField(3);

		JPanel panel = new JPanel();
		panel.add(MagicLabel);
		panel.add(MagicField);
		return panel;
	}

	private JPanel createStatusPanel() {
		JLabel StatusLabel = new JLabel("Status: ");
		StatusLabel.setHorizontalAlignment(JLabel.RIGHT);

		DarknessBox = new JCheckBox("Darkness");
		ZombieBox = new JCheckBox("Zombie");
		PoisonBox = new JCheckBox("Poison");
		MagiTekBox = new JCheckBox("MagiTek");
		InvisibleBox = new JCheckBox("Invisible");
		KappaBox = new JCheckBox("Kappa");
		StoneBox = new JCheckBox("Stone");
		WoundedBox = new JCheckBox("Wounded");
		FloatBox = new JCheckBox("Float");

		JPanel panel = new JPanel();

		panel.setLayout(new GridLayout(0,3));
		panel.add(StatusLabel);
		panel.add(DarknessBox);
		panel.add(ZombieBox);
		panel.add(new JPanel()); // spacer
		panel.add(PoisonBox);
		panel.add(MagiTekBox);
		panel.add(new JPanel());
		panel.add(InvisibleBox);
		panel.add(KappaBox);
		panel.add(new JPanel());
		panel.add(StoneBox);
		panel.add(WoundedBox);
		panel.add(new JPanel());
		panel.add(FloatBox);
		return panel;
	}

	private JPanel createCommandPanel(int n) {
		CommandLabel1 = new JLabel ("Command1: " + SaveState.getCommands(1));
		CommandLabel2 = new JLabel ("Command2: " + SaveState.getCommands(2));
		CommandLabel3 = new JLabel ("Command3: " + SaveState.getCommands(3));
		CommandLabel4 = new JLabel ("Command4: " + SaveState.getCommands(4));

		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		if (n == 1)
			panel.add(CommandLabel1, BorderLayout.WEST);
		else if (n == 2)
			panel.add(CommandLabel2, BorderLayout.WEST);
		else if (n == 3)
			panel.add(CommandLabel3, BorderLayout.WEST);
		else if (n == 4)
			panel.add(CommandLabel4, BorderLayout.WEST);
		
		return panel;
	}

	private JPanel createItemPanel() {
		JPanel EsperPanel = createEsperPanel();		
		JPanel RightItemPanel = createRightItemPanel();
		JPanel LeftItemPanel = createLeftItemPanel();

		JPanel HelmetPanel = createHelmetPanel();
		JPanel ArmorPanel = createArmorItemPanel();
		JPanel Relic1Panel = createRelic1Panel();
		JPanel Relic2Panel = createRelic2Panel();

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0,2));
		panel.add(EsperPanel);
		panel.add(RightItemPanel);
		panel.add(LeftItemPanel);
		panel.add(HelmetPanel);
		panel.add(ArmorPanel);
		panel.add(Relic1Panel);
		panel.add(Relic2Panel);
		return panel;
	}

	private JPanel createEsperPanel() {
		JLabel EsperLabel = new JLabel("Esper: ");

		EsperCombo = new JComboBox<String>();

		EsperCombo.addItem("None");		
		EsperCombo.addItem("Ramuh");
		EsperCombo.addItem("Ifrit");
		EsperCombo.addItem("Shiva");
		EsperCombo.addItem("Siren");
		EsperCombo.addItem("Terrato");
		EsperCombo.addItem("Maduin");
		EsperCombo.addItem("Shoat");
		EsperCombo.addItem("Bismark");
		EsperCombo.addItem("Stray");
		EsperCombo.addItem("Palidor");
		EsperCombo.addItem("Tritoch");
		EsperCombo.addItem("Odin");
		EsperCombo.addItem("Raiden");
		EsperCombo.addItem("Bahamut");
		EsperCombo.addItem("Alexandr");
		EsperCombo.addItem("Crusader");
		EsperCombo.addItem("Ragnarok");
		EsperCombo.addItem("Kirin");
		EsperCombo.addItem("Zoneseek");
		EsperCombo.addItem("Carbunkl");
		EsperCombo.addItem("Phantom");
		EsperCombo.addItem("Sraphim");
		EsperCombo.addItem("Golem");
		EsperCombo.addItem("Unicorn");
		EsperCombo.addItem("Fenrir");
		EsperCombo.addItem("Startlet");
		EsperCombo.addItem("Phoenix");

		EsperCombo.setSelectedItem("None");

		JPanel panel = new JPanel();
		panel.add(EsperLabel);
		panel.add(EsperCombo);
		return panel;
	}

	private JPanel createRightItemPanel() {
		RightItemCombo = new JComboBox<String>();

		RightItemCombo.addItem("EMPTY");
		RightItemCombo.addItem("Dagger");
		RightItemCombo.addItem("Mithril Knife");
		RightItemCombo.addItem("Main Gauche");
		RightItemCombo.addItem("Air Knife");
		RightItemCombo.addItem("Thief's Knife");
		RightItemCombo.addItem("Assassin Dagger");
		RightItemCombo.addItem("Man Eater");
		RightItemCombo.addItem("Sword Breaker");
		RightItemCombo.addItem("Gladius");
		RightItemCombo.addItem("Valiant Knife");
		RightItemCombo.addItem("Mithril Sword");
		RightItemCombo.addItem("Great Sword");
		RightItemCombo.addItem("Rune Blade");
		RightItemCombo.addItem("Flame Tongue");
		RightItemCombo.addItem("Ice Brand");
		RightItemCombo.addItem("Thunder Blade");
		RightItemCombo.addItem("Bastard Sword");
		RightItemCombo.addItem("Break Blade");
		RightItemCombo.addItem("Blood Sword");
		RightItemCombo.addItem("Enhance Sword");
		RightItemCombo.addItem("Crystal Sword");
		RightItemCombo.addItem("Falchion");
		RightItemCombo.addItem("Soul Saber");
		RightItemCombo.addItem("Ogre Nix");
		RightItemCombo.addItem("Excalibur");
		RightItemCombo.addItem("Zantetsuken");
		RightItemCombo.addItem("Lightbringer");
		RightItemCombo.addItem("Ragnarok");
		RightItemCombo.addItem("Ultima Weapon");
		RightItemCombo.addItem("Mitril Spear");
		RightItemCombo.addItem("Trident");
		RightItemCombo.addItem("Heavy Lance");
		RightItemCombo.addItem("Partisan");
		RightItemCombo.addItem("Holy Lance");
		RightItemCombo.addItem("Golden Spear");
		RightItemCombo.addItem("Glow Lance");
		RightItemCombo.addItem("Sagojou's Spear");
		RightItemCombo.addItem("Kunai");
		RightItemCombo.addItem("Kodachi");
		RightItemCombo.addItem("Sakurafubuki");
		RightItemCombo.addItem("Piercing Katana");
		RightItemCombo.addItem("Ichigeki");
		RightItemCombo.addItem("Kagenui");
		RightItemCombo.addItem("Ashura");
		RightItemCombo.addItem("Kotetsu");
		RightItemCombo.addItem("Kikuichimonji");
		RightItemCombo.addItem("Kazekiri Blade");
		RightItemCombo.addItem("Murasame");
		RightItemCombo.addItem("Masamune");
		RightItemCombo.addItem("Heaven's Cloud");
		RightItemCombo.addItem("Mutsu no Kami");
		RightItemCombo.addItem("Heal Rod");
		RightItemCombo.addItem("Mithril Rod");
		RightItemCombo.addItem("Flame Rod");
		RightItemCombo.addItem("Ice Rod");
		RightItemCombo.addItem("Thunder Rod");
		RightItemCombo.addItem("Poison Rod");
		RightItemCombo.addItem("Holy Rod");
		RightItemCombo.addItem("Gravity Rod");
		RightItemCombo.addItem("Punisher");
		RightItemCombo.addItem("Wizard Rod");
		RightItemCombo.addItem("Chocobo Brush");
		RightItemCombo.addItem("Da Vinci Brush");
		RightItemCombo.addItem("Magical Brush");
		RightItemCombo.addItem("Rainbow Brush");
		RightItemCombo.addItem("Shuriken");
		RightItemCombo.addItem("Demon Wind Shuriken");
		RightItemCombo.addItem("Windmill");
		RightItemCombo.addItem("Chain Flail");
		RightItemCombo.addItem("Full Moon");
		RightItemCombo.addItem("Morning Star");
		RightItemCombo.addItem("Boomerang");
		RightItemCombo.addItem("Rising Sun");
		RightItemCombo.addItem("Hawk Eye");
		RightItemCombo.addItem("Bone Club");
		RightItemCombo.addItem("Sniper");
		RightItemCombo.addItem("Wing Edge");
		RightItemCombo.addItem("Card");
		RightItemCombo.addItem("Darts");
		RightItemCombo.addItem("Death Card");
		RightItemCombo.addItem("Intense Darts");
		RightItemCombo.addItem("Dice");
		RightItemCombo.addItem("Fake Dice");
		RightItemCombo.addItem("Metal Knuckle");
		RightItemCombo.addItem("Mithril Claw");
		RightItemCombo.addItem("Kaiser Claw");
		RightItemCombo.addItem("Dark Claw");
		RightItemCombo.addItem("Burning Knuckle");
		RightItemCombo.addItem("Dragon Claw");
		RightItemCombo.addItem("Tiger Fang");
		RightItemCombo.setSelectedItem("EMPTY");

		JLabel RightItemLabel = new JLabel("Right: ");

		JPanel panel = new JPanel();
		panel.add(RightItemLabel);
		panel.add(RightItemCombo);
		return panel;
	}

	private JPanel createLeftItemPanel() {
		LeftItemCombo = new JComboBox<String>();

		LeftItemCombo.addItem("EMPTY");
		LeftItemCombo.addItem("Dagger");
		LeftItemCombo.addItem("Mithril Knife");
		LeftItemCombo.addItem("Main Gauche");
		LeftItemCombo.addItem("Air Knife");
		LeftItemCombo.addItem("Thief's Knife");
		LeftItemCombo.addItem("Assassin Dagger");
		LeftItemCombo.addItem("Man Eater");
		LeftItemCombo.addItem("Sword Breaker");
		LeftItemCombo.addItem("Gladius");
		LeftItemCombo.addItem("Valiant Knife");
		LeftItemCombo.addItem("Mithril Sword");
		LeftItemCombo.addItem("Great Sword");
		LeftItemCombo.addItem("Rune Blade");
		LeftItemCombo.addItem("Flame Tongue");
		LeftItemCombo.addItem("Ice Brand");
		LeftItemCombo.addItem("Thunder Blade");
		LeftItemCombo.addItem("Bastard Sword");
		LeftItemCombo.addItem("Break Blade");
		LeftItemCombo.addItem("Blood Sword");
		LeftItemCombo.addItem("Enhance Sword");
		LeftItemCombo.addItem("Crystal Sword");
		LeftItemCombo.addItem("Falchion");
		LeftItemCombo.addItem("Soul Saber");
		LeftItemCombo.addItem("Ogre Nix");
		LeftItemCombo.addItem("Excalibur");
		LeftItemCombo.addItem("Zantetsuken");
		LeftItemCombo.addItem("Lightbringer");
		LeftItemCombo.addItem("Ragnarok");
		LeftItemCombo.addItem("Ultima Weapon");
		LeftItemCombo.addItem("Mitril Spear");
		LeftItemCombo.addItem("Trident");
		LeftItemCombo.addItem("Heavy Lance");
		LeftItemCombo.addItem("Partisan");
		LeftItemCombo.addItem("Holy Lance");
		LeftItemCombo.addItem("Golden Spear");
		LeftItemCombo.addItem("Glow Lance");
		LeftItemCombo.addItem("Sagojou's Spear");
		LeftItemCombo.addItem("Kunai");
		LeftItemCombo.addItem("Kodachi");
		LeftItemCombo.addItem("Sakurafubuki");
		LeftItemCombo.addItem("Piercing Katana");
		LeftItemCombo.addItem("Ichigeki");
		LeftItemCombo.addItem("Kagenui");
		LeftItemCombo.addItem("Ashura");
		LeftItemCombo.addItem("Kotetsu");
		LeftItemCombo.addItem("Kikuichimonji");
		LeftItemCombo.addItem("Kazekiri Blade");
		LeftItemCombo.addItem("Murasame");
		LeftItemCombo.addItem("Masamune");
		LeftItemCombo.addItem("Heaven's Cloud");
		LeftItemCombo.addItem("Mutsu no Kami");
		LeftItemCombo.addItem("Heal Rod");
		LeftItemCombo.addItem("Mithril Rod");
		LeftItemCombo.addItem("Flame Rod");
		LeftItemCombo.addItem("Ice Rod");
		LeftItemCombo.addItem("Thunder Rod");
		LeftItemCombo.addItem("Poison Rod");
		LeftItemCombo.addItem("Holy Rod");
		LeftItemCombo.addItem("Gravity Rod");
		LeftItemCombo.addItem("Punisher");
		LeftItemCombo.addItem("Wizard Rod");
		LeftItemCombo.addItem("Chocobo Brush");
		LeftItemCombo.addItem("Da Vinci Brush");
		LeftItemCombo.addItem("Magical Brush");
		LeftItemCombo.addItem("Rainbow Brush");
		LeftItemCombo.addItem("Shuriken");
		LeftItemCombo.addItem("Demon Wind Shuriken");
		LeftItemCombo.addItem("Windmill");
		LeftItemCombo.addItem("Chain Flail");
		LeftItemCombo.addItem("Full Moon");
		LeftItemCombo.addItem("Morning Star");
		LeftItemCombo.addItem("Boomerang");
		LeftItemCombo.addItem("Rising Sun");
		LeftItemCombo.addItem("Hawk Eye");
		LeftItemCombo.addItem("Bone Club");
		LeftItemCombo.addItem("Sniper");
		LeftItemCombo.addItem("Wing Edge");
		LeftItemCombo.addItem("Card");
		LeftItemCombo.addItem("Darts");
		LeftItemCombo.addItem("Death Card");
		LeftItemCombo.addItem("Intense Darts");
		LeftItemCombo.addItem("Dice");
		LeftItemCombo.addItem("Fake Dice");
		LeftItemCombo.addItem("Metal Knuckle");
		LeftItemCombo.addItem("Mithril Claw");
		LeftItemCombo.addItem("Kaiser Claw");
		LeftItemCombo.addItem("Dark Claw");
		LeftItemCombo.addItem("Burning Knuckle");
		LeftItemCombo.addItem("Dragon Claw");
		LeftItemCombo.addItem("Tiger Fang");
		LeftItemCombo.addItem("Buckler");
		LeftItemCombo.addItem("Large Shield");
		LeftItemCombo.addItem("Mithril Shield");
		LeftItemCombo.addItem("Gold Shield");
		LeftItemCombo.addItem("Aegis Shield");
		LeftItemCombo.addItem("Diamond Shield");
		LeftItemCombo.addItem("Flame Shield");
		LeftItemCombo.addItem("Ice Shield");
		LeftItemCombo.addItem("Thunder Shield");
		LeftItemCombo.addItem("Crystal Shield");
		LeftItemCombo.addItem("Genji Shield");
		LeftItemCombo.addItem("Shell Shield");
		LeftItemCombo.addItem("Bloodstained Shield");
		LeftItemCombo.addItem("Hero Shield");
		LeftItemCombo.addItem("Force Shield");

		LeftItemCombo.setSelectedItem("EMPTY");

		JLabel LeftItemLabel = new JLabel("Left: ");

		JPanel panel = new JPanel();
		panel.add(LeftItemLabel);
		panel.add(LeftItemCombo);
		return panel;
	}

	private JPanel createHelmetPanel() {
		JLabel HelmetLabel = new JLabel("Helmet: ");

		HelmetCombo = new JComboBox<String>();

		HelmetCombo.addItem("EMPTY");
		HelmetCombo.addItem("Leather Hat");		
		HelmetCombo.addItem("Hairband");
		HelmetCombo.addItem("Feathered Hat");
		HelmetCombo.addItem("Beret");
		HelmetCombo.addItem("Triangular Hat");
		HelmetCombo.addItem("Bandana");
		HelmetCombo.addItem("Iron Helm");
		HelmetCombo.addItem("Hypno Crown");
		HelmetCombo.addItem("Reason Hat");
		HelmetCombo.addItem("Green Beret");
		HelmetCombo.addItem("Head Band");
		HelmetCombo.addItem("Mithril Helm");
		HelmetCombo.addItem("Tiara");
		HelmetCombo.addItem("Gold Helm");
		HelmetCombo.addItem("Tiger Mask");
		HelmetCombo.addItem("Red Cap");
		HelmetCombo.addItem("Mystery Veil");
		HelmetCombo.addItem("Circlet");
		HelmetCombo.addItem("Royal Crown");
		HelmetCombo.addItem("Diamond Helmet");
		HelmetCombo.addItem("Black Hood");
		HelmetCombo.addItem("Crystal Helm");
		HelmetCombo.addItem("Oath Veil");
		HelmetCombo.addItem("Cat Ear Hood");
		HelmetCombo.addItem("Genji Helmet");
		HelmetCombo.addItem("Thorn Crown");
		HelmetCombo.addItem("Dish");

		HelmetCombo.setSelectedItem("EMPTY");

		JPanel panel = new JPanel();
		panel.add(HelmetLabel);
		panel.add(HelmetCombo);
		return panel;
	}

	private JPanel createArmorItemPanel() {
		JLabel ArmorLabel = new JLabel("Armor: ");

		ArmorCombo = new JComboBox<String>();

		ArmorCombo.addItem("EMPTY");
		ArmorCombo.addItem("LeatherArmor");		
		ArmorCombo.addItem("Cotton Robe");
		ArmorCombo.addItem("Kempo Gear");
		ArmorCombo.addItem("Iron Armor");
		ArmorCombo.addItem("Silk Robe");
		ArmorCombo.addItem("Mithril Plate");
		ArmorCombo.addItem("Shinobi Gear");
		ArmorCombo.addItem("White Dress");
		ArmorCombo.addItem("Mithril Mail");
		ArmorCombo.addItem("Earth Gear");
		ArmorCombo.addItem("Mirage Vest");
		ArmorCombo.addItem("Gold Armor");
		ArmorCombo.addItem("Strength Vest");
		ArmorCombo.addItem("Light Robe");
		ArmorCombo.addItem("Diamond Breastplate");
		ArmorCombo.addItem("Red Jacket");
		ArmorCombo.addItem("Force Armor");
		ArmorCombo.addItem("Diamond Armor");
		ArmorCombo.addItem("Black Clothes");
		ArmorCombo.addItem("Comradeship Robe");
		ArmorCombo.addItem("Crystal Armor");
		ArmorCombo.addItem("Princess Gown");
		ArmorCombo.addItem("Genji Armor");
		ArmorCombo.addItem("Kappa Armor");
		ArmorCombo.addItem("Minerva Bustier");
		ArmorCombo.addItem("Kitty Suit");
		ArmorCombo.addItem("Chocobo Suit");
		ArmorCombo.addItem("Moglie Suit");
		ArmorCombo.addItem("Nutkin Suit");
		ArmorCombo.addItem("Behemoth Suit");
		ArmorCombo.addItem("Snow Muffler");

		ArmorCombo.setSelectedItem("EMPTY");

		JPanel panel = new JPanel();
		panel.add(ArmorLabel);
		panel.add(ArmorCombo);
		return panel;
	}

	private JPanel createRelic1Panel() {
		JLabel Relic1Label = new JLabel("Relic 1: ");
		Relic1Combo = new JComboBox<String>();

		Relic1Combo.addItem("EMPTY");
		Relic1Combo.addItem("Silver Glasses");		
		Relic1Combo.addItem("Star Pendant");
		Relic1Combo.addItem("Peace Ring");
		Relic1Combo.addItem("Amulet");
		Relic1Combo.addItem("White Cape");
		Relic1Combo.addItem("Jewel Ring");
		Relic1Combo.addItem("Fairy Ring");
		Relic1Combo.addItem("Barrier Ring");
		Relic1Combo.addItem("Mithril Glove");
		Relic1Combo.addItem("Protect Ring");
		Relic1Combo.addItem("Hermes Shoes");
		Relic1Combo.addItem("Reflect Ring");
		Relic1Combo.addItem("Angel Feathers");
		Relic1Combo.addItem("Angel Ring");
		Relic1Combo.addItem("Knight's Code");
		Relic1Combo.addItem("Dragon Knight Boots");
		Relic1Combo.addItem("Wind Mantle");
		Relic1Combo.addItem("Princess Ring");
		Relic1Combo.addItem("Curse Ring");
		Relic1Combo.addItem("Earring");
		Relic1Combo.addItem("Giant Glove");
		Relic1Combo.addItem("Storm Orb");
		Relic1Combo.addItem("Rage Ring");
		Relic1Combo.addItem("Thief Bangle");
		Relic1Combo.addItem("Defense Bangle");
		Relic1Combo.addItem("Brave Ring");
		Relic1Combo.addItem("Ribbon");
		Relic1Combo.addItem("Muscle Belt");
		Relic1Combo.addItem("Crystal Orb");		
		Relic1Combo.addItem("Gold Hairpin");
		Relic1Combo.addItem("Three Stars");
		Relic1Combo.addItem("Thief Glove");
		Relic1Combo.addItem("Gauntlet");
		Relic1Combo.addItem("Genji Glove");
		Relic1Combo.addItem("Hyper Wrist");
		Relic1Combo.addItem("Warrior's Proof");
		Relic1Combo.addItem("Abandon Beads");
		Relic1Combo.addItem("Black Belt");
		Relic1Combo.addItem("Heiji's Cross");
		Relic1Combo.addItem("Dancho's Mustache");
		Relic1Combo.addItem("Soul of Thamasa");
		Relic1Combo.addItem("Flying Dragon Horn");
		Relic1Combo.addItem("Medal");
		Relic1Combo.addItem("Memento Ring");
		Relic1Combo.addItem("Safety Bit");
		Relic1Combo.addItem("Deceased Ring");
		Relic1Combo.addItem("Morul's Charm");
		Relic1Combo.addItem("Repelling Bracelet");
		Relic1Combo.addItem("Miracle Shoes");
		Relic1Combo.addItem("Alarm Pierce");
		Relic1Combo.addItem("Gale Hairpin");
		Relic1Combo.addItem("Sniper Eye");
		Relic1Combo.addItem("Grow Egg");
		Relic1Combo.addItem("Cat's Bell");
		Relic1Combo.addItem("Dash Shoes");

		Relic1Combo.setSelectedItem("EMPTY");

		JPanel panel = new JPanel();
		panel.add(Relic1Label);
		panel.add(Relic1Combo);
		return panel;
	}

	private JPanel createRelic2Panel() {
		JLabel Relic2Label = new JLabel("Relic 2: ");
		Relic2Combo = new JComboBox<String>();

		Relic2Combo.addItem("EMPTY");
		Relic2Combo.addItem("Silver Glasses");		
		Relic2Combo.addItem("Star Pendant");
		Relic2Combo.addItem("Peace Ring");
		Relic2Combo.addItem("Amulet");
		Relic2Combo.addItem("White Cape");
		Relic2Combo.addItem("Jewel Ring");
		Relic2Combo.addItem("Fairy Ring");
		Relic2Combo.addItem("Barrier Ring");
		Relic2Combo.addItem("Mithril Glove");
		Relic2Combo.addItem("Protect Ring");
		Relic2Combo.addItem("Hermes Shoes");
		Relic2Combo.addItem("Reflect Ring");
		Relic2Combo.addItem("Angel Feathers");
		Relic2Combo.addItem("Angel Ring");
		Relic2Combo.addItem("Knight's Code");
		Relic2Combo.addItem("Dragon Knight Boots");
		Relic2Combo.addItem("Wind Mantle");
		Relic2Combo.addItem("Princess Ring");
		Relic2Combo.addItem("Curse Ring");
		Relic2Combo.addItem("Earring");
		Relic2Combo.addItem("Giant Glove");
		Relic2Combo.addItem("Storm Orb");
		Relic2Combo.addItem("Rage Ring");
		Relic2Combo.addItem("Thief Bangle");
		Relic2Combo.addItem("Defense Bangle");
		Relic2Combo.addItem("Brave Ring");
		Relic2Combo.addItem("Ribbon");
		Relic2Combo.addItem("Muscle Belt");
		Relic2Combo.addItem("Crystal Orb");		
		Relic2Combo.addItem("Gold Hairpin");
		Relic2Combo.addItem("Three Stars");
		Relic2Combo.addItem("Thief Glove");
		Relic2Combo.addItem("Gauntlet");
		Relic2Combo.addItem("Genji Glove");
		Relic2Combo.addItem("Hyper Wrist");
		Relic2Combo.addItem("Warrior's Proof");
		Relic2Combo.addItem("Abandon Beads");
		Relic2Combo.addItem("Black Belt");
		Relic2Combo.addItem("Heiji's Cross");
		Relic2Combo.addItem("Dancho's Mustache");
		Relic2Combo.addItem("Soul of Thamasa");
		Relic2Combo.addItem("Flying Dragon Horn");
		Relic2Combo.addItem("Medal");
		Relic2Combo.addItem("Memento Ring");
		Relic2Combo.addItem("Safety Bit");
		Relic2Combo.addItem("Deceased Ring");
		Relic2Combo.addItem("Morul's Charm");
		Relic2Combo.addItem("Repelling Bracelet");
		Relic2Combo.addItem("Miracle Shoes");
		Relic2Combo.addItem("Alarm Pierce");
		Relic2Combo.addItem("Gale Hairpin");
		Relic2Combo.addItem("Sniper Eye");
		Relic2Combo.addItem("Grow Egg");
		Relic2Combo.addItem("Cat's Bell");
		Relic2Combo.addItem("Dash Shoes");

		Relic2Combo.setSelectedItem("EMPTY");

		JPanel panel = new JPanel();
		panel.add(Relic2Label);
		panel.add(Relic2Combo);
		return panel;
	}

	private JPanel createButton() {
		saveButton = new JButton("Save");
		this.frame = frame;
		class SaveListener implements ActionListener {
			public void actionPerformed(ActionEvent event) {
				try{
					path = (String) PathField.getText();
					String Character = (String) CharacterSelect.getSelectedItem();
					SaveState.SetOffset(Character);
					SaveState.fileRead(path);
					int level = Integer.parseInt(LevelField.getText());
					int curHP = Integer.parseInt(curHealthField.getText());
					int maxHP = Integer.parseInt(maxHealthField.getText());
					int curMP = Integer.parseInt(curMagicField.getText());
					int maxMP = Integer.parseInt(maxMagicField.getText());
					int exp = Integer.parseInt(ExpField.getText());
					int strength = Integer.parseInt(StrengthField.getText());
					int speed = Integer.parseInt(SpeedField.getText());
					int vitality = Integer.parseInt(VitalityField.getText());
					int magic = Integer.parseInt(MagicField.getText());
					String esper = (String) EsperCombo.getSelectedItem();
					SaveState.setEsper(esper);
					SaveState.setRightItem((String)RightItemCombo.getSelectedItem());
					SaveState.setLeftItem((String)LeftItemCombo.getSelectedItem());
					SaveState.setHelmetItem((String)HelmetCombo.getSelectedItem());
					SaveState.setArmorItem((String)ArmorCombo.getSelectedItem());
					SaveState.setRelic1Item((String)Relic1Combo.getSelectedItem());
					SaveState.setRelic2Item((String)Relic2Combo.getSelectedItem());
					
					if (DarknessBox.isSelected())
						StatusByte = (byte)(StatusByte | 0x01);
					else StatusByte &= (byte)0xFE;
					if (ZombieBox.isSelected())
						StatusByte = (byte)(StatusByte | 0x02);
					else StatusByte &= (byte)0xFD;
					if (PoisonBox.isSelected())
						StatusByte = (byte)(StatusByte | 0x04);
					else StatusByte &= (byte)0xFB;
					if (MagiTekBox.isSelected())
						StatusByte = (byte)(StatusByte | 0x08);
					else StatusByte &= (byte)0xF7;
					if (InvisibleBox.isSelected())
						StatusByte = (byte)(StatusByte | 0x10);
					else StatusByte &= (byte)0xEF;
					if (KappaBox.isSelected())
						StatusByte = (byte)(StatusByte | 0x20);
					else StatusByte &= (byte)0xDF;
					if (StoneBox.isSelected())
						StatusByte = (byte)(StatusByte | 0x40);
					else StatusByte &= (byte)0xBF;
					if (WoundedBox.isSelected())
						StatusByte = (byte)(StatusByte | 0x80);
					else StatusByte &= (byte)0x7F;
					if (FloatBox.isSelected())
						SaveState.setFloat(true);
					else SaveState.setFloat(false);

					SaveState.setStatus(StatusByte);
					
					int gil = Integer.parseInt(GilField.getText());
					SaveState.setGil(gil);

					if (level > 99 || level < 1) {
						JOptionPane.showMessageDialog(frame, "Level must be between 1-99.");
						return;
					}
					else if (maxHP > 9999 || maxHP < 0) {
						JOptionPane.showMessageDialog(frame, "Max HP must be between 1-9999.");
						return;
					}
					else if (curHP > maxHP) {
						JOptionPane.showMessageDialog(frame, "Current HP is greater than the max HP.");
						return;
					}
					else if (maxMP > 999 || maxMP < 0) {
						JOptionPane.showMessageDialog(frame, "Max MP must be between 1-999.");
						return;
					}
					else if (curMP > maxMP) {
						JOptionPane.showMessageDialog(frame, "Current MP is greater than the max MP.");
						return;
					}
					else if (strength > 255 || strength < 0) {
						JOptionPane.showMessageDialog(frame, "Strength must be between 1-255.");
						return;
					}
					else if (speed > 255 || speed < 0) {
						JOptionPane.showMessageDialog(frame, "Speed must be between 1-255.");
						return;
					}
					else if (vitality > 255 || vitality < 0) {
						JOptionPane.showMessageDialog(frame, "Vitality must be between 1-255.");
						return;
					}
					else if (magic > 255 || magic < 0) {
						JOptionPane.showMessageDialog(frame, "Magic must be between 1-255.");
						return;
					}
					else if (gil > 65535 || gil < 0) {
						JOptionPane.showMessageDialog(frame, "Gil must be between 1-65535.");
					}
					else {
						SaveState.fileWrite(level, curHP, maxHP, curMP, maxMP, exp, strength, speed, vitality, magic);
						JOptionPane.showMessageDialog(frame, "Save State successfully saved.");
					}
				}
				catch (IOException e) {
					System.exit(1);
				}
			}
		}

		ActionListener saveClick = new SaveListener();
		saveButton.addActionListener(saveClick);

		cancelButton = new JButton("Cancel");

		class CancelListener implements ActionListener {
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		}
		
		ActionListener cancelClick = new CancelListener();
		cancelButton.addActionListener(cancelClick);

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1, 2));
		buttonPanel.add(saveButton);
		buttonPanel.add(cancelButton);

		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.add(buttonPanel, BorderLayout.EAST);
		return panel;
	}
}
